﻿namespace TwiterApi.Tests
{
    using System;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using TwitterApi;

    [TestClass]
    public class TimelineTests
    {
        private const string ConsumerKey = "iSzCAq21MlWLyzebmZOyUg";
        private const string ConsumerSecret = "TgURTF38WpW7JkQReKcxjoaA7LrflTdacYAB6ulfw";

        private static OAuthToken token = new OAuthToken()
        {           
            Token = "102372218-l4ArNeEjPM3gqb93dwLB0ZqiH3LlHWNeF3qbbgd1",
            Secret = "dnlIbZ0fxkeSSQDYMpL2MBK0X7Coa0lRgM2SYEGfZfg"
        };

        [TestMethod]
        public void ShouldRetrievePublicTimeline()
        {
            var timeline = Proxy.GetPublicTimeline();

            Assert.IsNotNull(timeline);
            Assert.IsTrue(timeline.Count > 0);
        }

        [TestMethod]
        public void ShouldRetrievePublicTimelineWithUserDataTrimmed()
        {
            var timeline = Proxy.GetPublicTimeline(trimUser: true);

            Assert.IsNull(timeline[0].user.name);
        }

        [TestMethod]
        public void ShouldRetrievePublicTimelineWithEntities()
        {
            var timeline = Proxy.GetPublicTimeline(includeEntities: true);

            Assert.IsNotNull(timeline[0].entities);
        }

        [TestMethod]
        public void ShouldRetrieveHomeTimeline()
        {
            var timeline = Proxy.GetHomeTimeline(ConsumerKey, ConsumerSecret, token);

            Assert.IsNotNull(timeline);
            Assert.IsTrue(timeline.Count > 0);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ShouldHomeTimelineThrowExceptionIfInvalidTokenSpecified()
        {
            var invalidToken = new OAuthToken()
            {           
                Token = "invalid-token",
                Secret = "invalid-secret"
            };

            var mentions = Proxy.GetHomeTimeline(ConsumerKey, ConsumerSecret, invalidToken);
        }

        [TestMethod]
        public void ShouldRetrieveFriendsTimeline()
        {
            var timeline = Proxy.GetFriendsTimeline(ConsumerKey, ConsumerSecret, token);

            Assert.IsNotNull(timeline);
            Assert.IsTrue(timeline.Count > 0);
        }

        [TestMethod]
        public void ShouldRetrieveUserTimelineFromAuthenticatingUser()
        {
            var timeline = Proxy.GetUserTimeline(ConsumerKey, ConsumerSecret, token);

            Assert.IsNotNull(timeline);
            Assert.IsTrue(timeline.Count > 0);
        }        
        
        [TestMethod]
        public void ShouldRetrieveUserTimelineFromSpecifiedUserScreenName()
        {
            var timeline = Proxy.GetUserTimeline(screenName: "webapptoolkits1");

            Assert.IsNotNull(timeline);
            Assert.IsTrue(timeline.Count > 0);
        }

        [TestMethod]
        public void ShouldRetrieveUserTimelineFromSpecifiedUserId()
        {
            var timeline = Proxy.GetUserTimeline(userId: 102372218);

            Assert.IsNotNull(timeline);
            Assert.IsTrue(timeline.Count > 0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ShouldUserTimelineThrowExceptionIfUserNotSpecified()
        {
            var timeline = Proxy.GetUserTimeline();
        }

        [TestMethod]
        public void ShouldRetrieveMentions()
        {           
            var mentions = Proxy.GetMentions(ConsumerKey, ConsumerSecret, token);

            Assert.IsNotNull(mentions);
            Assert.IsTrue(mentions.Count > 0);
        }

        [TestMethod]
        public void ShouldRetrieveRetweetedByMe()
        {
            var retweets = Proxy.GetRetweetedByMe(ConsumerKey, ConsumerSecret, token);

            Assert.IsNotNull(retweets);
            Assert.IsTrue(retweets.Count > 0);
            Assert.IsTrue(retweets.First().text.Contains("RT"));
        }

        [TestMethod]
        public void ShouldRetrieveRetweetedToMe()
        {
            var retweets = Proxy.GetRetweetedToMe(ConsumerKey, ConsumerSecret, token);

            Assert.IsNotNull(retweets);
            Assert.IsTrue(retweets.Count > 0);
            Assert.IsTrue(retweets.First().text.Contains("RT"));
        }

        [TestMethod]
        public void ShouldRetrieveRetweetsOfMe()
        {
            var retweets = Proxy.GetRetweetsOfMe(ConsumerKey, ConsumerSecret, token);

            Assert.IsNotNull(retweets);
            Assert.IsTrue(retweets.Count > 0);            
        }
    }
}